<template>
  <div class="container" ref="mainCon">
    <!-- <el-row class="titleBox clearfix">
            <span class="blockTitle">设备监控管理</span>
            <el-button type="primary" round class="listAddBtn fr" v-show="isShowAdd" @click="addEquipment">添加
            </el-button>
        </el-row> -->
    <topTitle titleText="设备监控管理"></topTitle>
    <el-button
      type="primary"
      round
      class="listAddBtn fr"
      v-show="isShowAdd"
      @click="addEquipment"
      >添加</el-button
    >
    <div class="mainCon">
      <el-row class="searchBox searchInputWidth">
        <el-form :inline="true" :model="filterField" ref="accountForm">
          <el-col :lg="8" :xl="6">
            <el-form-item label="企业名称" class="row-padding-bottom">
              <el-input
                placeholder="输入企业名称"
                clearable
                v-model="filterField.companyName"
                @blur="filterField.companyName = changeValue($event)"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="设备类型" class="row-padding-bottom">
              <el-select
                v-model="filterField.type"
                placeholder="请选择"
                clearable
              >
                <!-- <el-option label="捷成安动态识别" value="01"></el-option> -->
                <el-option
                  v-for="(item, index) of typeData"
                  :key="index"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="识别号码" class="row-padding-bottom">
                <el-input
                  placeholder="输入识别号码"
                  clearable
                  v-model="filterField.machineNo"
                  @blur="filterField.machineNo = changeValue($event)"
                />
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="在离线状态" class="row-padding-bottom">
                <el-select
                  placeholder="请选择"
                  v-model="filterField.status"
                  clearable
                >
                  <el-option label="在线" value="1" />
                  <el-option label="离线" value="0" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="更新时间" class="row-padding-bottom">
                <el-date-picker
                  v-model="filterField.lastDate"
                  type="date"
                  placeholder="请选择"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchTable"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :dataLen="tableData.length"
                :filterField="filterField"
                api="api-s/report/export_SYS_SET"
              ></exportBtn>
            </el-form-item>
          </el-col>
          <!-- <el-form-item>
                        <el-button
                            type="primary"
                            icon="icon iconfont iconchazhao1"
                            @click="searchTable"
                            class="commonBtn"
                        />
                    </el-form-item> -->
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <!-- 列表 -->
        <el-table
          ref="tableBox1"
          :max-height="tableHeight"
          :data="tableData"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          v-loading="loading"
          style="border: 1px solid #dfe6ec"
        >
          <!-- <el-table-column align="center" prop="companyName" label="企业名称" :formatter="formatTd"/> -->
          <el-table-column label="企业名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.companyName"
                :columnName="'companyName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="typeValue"
            label="设备类型"
            :formatter="formatTd"
            width="180"
          />
          <el-table-column
            align="center"
            prop="machineNo"
            label="设备识别号码"
            :formatter="formatTd"
            width="150"
          />
          <el-table-column
            align="center"
            prop="authCode"
            label="授权码"
            :formatter="formatTd"
            width="150"
          />
          <el-table-column
            align="center"
            prop="directionValue"
            label="进出方向"
            :formatter="formatTd"
            width="150"
          />
          <el-table-column
            align="center"
            prop="hardwareIp"
            label="网络IP"
            :formatter="formatTd"
            width="150"
          />
          <el-table-column
            align="center"
            prop="statusValue"
            label="在离线状态"
            :formatter="formatTd"
            width="150"
          />
          <el-table-column
            align="center"
            prop="lastDate"
            label="最后一次考勤上传时间"
            :formatter="formatTd"
            width="280"
          />
          <el-table-column
            align="center"
            prop="remark"
            label="备注"
            :formatter="formatTd"
          />
          <el-table-column
            align="left"
            label="操作"
            width="100px"
            fixed="right"
          >
            <template slot-scope="scope">
              <div v-if="isShowUpdate || isShowDelete">
                <el-link
                  type="primary"
                  @click="updateRow(scope.row)"
                  v-show="isShowUpdate"
                  >修改</el-link
                >
                <el-link
                  type="primary"
                  @click="deleteRow(scope.row.id)"
                  v-show="isShowDelete"
                  >删除</el-link
                >
                <el-link
                  type="primary"
                  v-show="scope.row.type == 'zk' || scope.row.type == 'yf'"
                  @click="showDetail(scope.row)"
                  >查看
                </el-link>
              </div>
              <div v-else>
                <span type="primary">--</span>
              </div>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息" />
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
        />
      </div>
    </div>
    <!--添加,修改-->
    <el-dialog
      :title="addOrEdit == 0 ? '考勤设备添加' : '考勤设备修改'"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
      @close="handleCloseForm"
      custom-class="dialog-minwidth"
      :modal-append-to-body="false"
    >
      <el-form
        :model="formData"
        label-width="80px"
        :rules="formRules"
        ref="formData"
        class="form"
      >
        <el-row class="row-padding-bottom">
          <el-col :span="24" class="col-position">
            <el-form-item
              label="企业名称"
              prop="companyId"
              class="row-padding-bottom"
              v-if="addOrEdit == 0"
            >
              <el-select
                v-model="formData.companyId"
                clearable
                filterable
                placeholder="请选择"
                :filter-method="selectChange"
                class="selectWidth"
                @change="handleChange"
                :disabled="isDisabled"
              >
                <el-option
                  v-for="(item, index) in companyList"
                  :key="index"
                  :label="item.companyName"
                  :value="item.companyId"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="企业名称" v-else style="margin-bottom: 0px">
              {{ formData.companyName || '--' }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" class="col-position">
            <el-form-item
              label="设备类型"
              class="row-padding-bottom"
              prop="type"
            >
              <el-select
                v-model="formData.type"
                placeholder="请选择"
                clearable
                class="selectWidth"
                @change="typeSelectChange"
              >
                <!-- <el-option label="捷成安动态识别" value="1"></el-option> -->
                <el-option
                  v-for="(item, index) of typeData"
                  :key="index"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="col-position">
            <el-form-item
              label="识别号码"
              class="row-padding-bottom"
              prop="machineNo"
            >
              <el-input
                placeholder="输入识别号码"
                clearable
                v-model="formData.machineNo"
                @blur="formData.machineNo = changeValue($event)"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="row-padding-bottom">
          <el-col :span="24" class="col-position">
            <el-form-item label="授权码" prop="authCode">
              <el-input
                placeholder="请输入授权码"
                v-model="formData.authCode"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" class="col-position">
            <el-form-item
              label="进出方向"
              class="row-padding-bottom"
              prop="direction"
            >
              <el-select
                v-model="formData.direction"
                placeholder="请选择"
                clearable
                class="selectWidth"
              >
                <el-option label="无方向" value="-1" />
                <el-option label="出门" value="0" />
                <el-option label="进门" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" class="col-position">
            <el-form-item label="工点授权" class="row-padding-bottom">
              <el-select
                v-model="formData.pointId"
                placeholder="请选择"
                clearable
                class="selectWidth"
              >
                <el-option
                  v-for="(item, index) of points"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="row-padding-bottom">
          <el-col :span="24" class="col-position">
            <el-form-item label="备注">
              <el-input
                type="textarea"
                placeholder="请输入备注"
                v-model="formData.remark"
                maxlength="100"
                row="3"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCloseForm" class="cancel"
          >取消</el-button
        >
        <el-button
          type="primary"
          @click.native="toSave"
          class="save"
          :loading="isRuleLoading"
          >保存</el-button
        >
      </div>
    </el-dialog>
    <zktecoMachine ref="zktecoMachine" />
    <yfMachine ref="yfMachine" />
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
import zktecoMachine from '@/components/operationManage/zktecoMachine'
import yfMachine from '@/components/operationManage/yfMachine'
export default {
  name: 'equipmentMonitorNC',
  mixins: [scrollTable],
  components: {
    zktecoMachine,
    yfMachine,
  },
  data() {
    return {
      requestUrl: 'api-att/nc/mac/expand/list', //表格请求路径
      advanced: false,
      loading: false,
      isShow: false,
      tableData: [], //表格数据
      filterField: {
        companyName: '',
        type: '',
        machineNo: '',
        status: '',
        lastDate: '',
      },
      pageSize: 20,
      pageNo: 1,
      companyList: [], //项目名称下来列表
      pageSizeNumber: 20,
      total: 0,
      pageNo: 1,
      companyId: '',
      addOrEdit: '',
      formVisible: false,
      formData: {
        id: '',
        companyId: '',
        companyName: '',
        pointId: '',
        type: '',
        machineNo: '',
        direction: '',
        authCode: '',
        remark: '',
      },
      formRules: {
        companyId: [
          { required: true, message: '项目名称不能为空', trigger: 'change' },
        ],
        type: [
          { required: true, message: '设备类型不能为空', trigger: 'change' },
        ],
        machineNo: [
          { required: true, message: '请输入正确的识别号', trigger: 'blur' },
        ],
        authCode: [
          { required: true, message: '授权码不能为空', trigger: 'blur' },
        ],
        direction: [
          { required: true, message: '进出方向不能为空', trigger: 'change' },
        ],
      },
      isRuleLoading: false,
      isDisabled: false,
      usccDisabled: false,
      id: '',
      typeData: '',
      companyNameUscc: '', //修改赋值的uscc
      projectNameId: '', //修改赋值的projectId
      zkIsShow: false,
      points: [], //工点列表
      isShowUpdate: '',
      isShowDelete: '',
      isShowAdd: '',
    }
  },
  created() {
    this.getType()
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.isShowUpdate = true //this.$store.state.buttonLimitsArr.indexOf('equipmentMonitor:update') > -1;
    this.isShowDelete = true //this.$store.state.buttonLimitsArr.indexOf('equipmentMonitor:delete') > -1;
    this.isShowAdd = true //this.$store.state.buttonLimitsArr.indexOf('equipmentMonitor:add') > -1;
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    // 获取设备类型
    getType() {
      this.axios({
        method: 'post',
        data: { type: 'MACHINE_TYPE' },
        url: 'api-bs/base/dicitem/findbytype',
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.typeData = response.data.result
            } else {
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    getTableList(data) {
      this.tableData = data
      this.loading = false
      if (this.tableData.length < 1) {
        this.isShow = true
      }
    },
    //加载项目列表
    getCompanyList() {
      this.axios({
        method: 'post',
        url: 'api-att/nc/attrule/expand/findcomp',
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.companyList = response.data.result
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    selectChange(val) {},
    handleSizeChange(val) {},
    handleCurrentChange(val) {
      this.getCompanyList(val, this.pageSizeNumber, this.companyId)
    },
    //列表查询
    searchTable() {
      this.$refs.pagination.getTableListData()
    },
    // 点击添加
    addEquipment() {
      this.pageNo = 1
      this.getCompanyList()
      this.addOrEdit = 0
      this.formVisible = true
      this.isDisabled = false
      this.usccDisabled = false
      this.formData.remark = ''
      this.formData.pointId = ''
      this.getType()
      this.$nextTick(function () {
        this.$refs['formData'].resetFields()
      })
    },
    //删除一行
    deleteRow(id) {
      this.axios({
        method: 'post',
        url: 'api-att/nc/mac/expand/delete',
        data: {
          id: id,
        },
      })
        .then(
          (response) => {
            if (!response.data.hasError) {
              this.$message({
                message: '删除成功',
                type: 'success',
              })
              this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            } else {
              this.$message({
                message: response.data.errorMsg,
                type: 'error',
              })
            }
          },
          (response) => {}
        )
        .catch((error) => {})
    },
    //修改一行
    updateRow(row) {
      this.addOrEdit = 1
      this.formVisible = true
      this.getType()
      this.isDisabled = true
      this.usccDisabled = true
      // this.getCompanyList();
      // this.getUpdateData(id);
      this.formData.type = row.type
      this.formData.machineNo = row.machineNo
      this.formData.direction = row.direction
      this.formData.remark = row.remark
      this.formData.companyId = row.companyId
      this.formData.companyName = row.companyName
      this.formData.authCode = row.authCode
      this.formData.pointId = row.pointId
      this.formData.id = row.id
      this.getPoints(this.companyId)
    },
    handleCloseForm() {
      this.formVisible = false
      // this.$refs["formData"].resetFields();
    },
    toSave() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          this.isRuleLoading = true
          this.EquipmentData()
        } else {
          return false
        }
      })
    },
    handleChange(val) {
      let obj = {}
      obj = this.companyList.find((item) => {
        return item.companyId === val
      })
      this.formData.companyName = obj.companyName
      this.getPoints(val)
    },

    EquipmentData() {
      var url, data, message
      if (this.addOrEdit == '0') {
        url = 'api-att/nc/mac/expand/add'
        data = {
          companyId: this.formData.companyId,
          companyName: this.formData.companyName,
          remark: this.formData.remark,
          type: this.formData.type,
          machineNo: this.formData.machineNo,
          direction: this.formData.direction,
          pointId: this.formData.pointId,
          authCode: this.formData.authCode,
        }
        message = '添加成功'
      } else {
        url = 'api-att/nc/mac/expand/update'
        data = {
          id: this.formData.id,
          companyId: this.formData.companyId,
          companyName: this.formData.companyName,
          remark: this.formData.remark,
          type: this.formData.type,
          machineNo: this.formData.machineNo,
          direction: this.formData.direction,
          pointId: this.formData.pointId,
          authCode: this.formData.authCode,
        }
        message = '修改成功'
      }
      this.axios({
        method: 'post',
        url: url,
        data: data,
      })
        .then((res) => {
          this.isRuleLoading = false
          if (!res.data.hasError) {
            this.$message({
              message: message,
              type: 'success',
            })
            this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
            this.formVisible = false
          } else {
            this.$message({
              message: res.data.errorMsg,
              type: 'error',
            })
          }
        })
        .catch(() => {
          this.isRuleLoading = false
          this.$message.error('添加数据失败')
        })
    },
    // 获取修改详情
    getUpdateData(id) {
      this.id = id
      this.axios({
        method: 'post',
        url: 'api-att/cs/mac/expand/detail',
        data: { id: id },
      })
        .then((res) => {
          if (!res.data.hasError) {
            var allData = res.data.result
            this.formData.type = allData.type
            this.formData.machineNo = allData.machineNo
            this.formData.direction = allData.direction
            this.formData.remark = allData.remark
            this.formData.projectName = allData.projectName
            this.formData.companyName = allData.companyName
            this.projectNameId = allData.projectId
            this.companyNameUscc = allData.uscc
            if ('zk' == allData.type) {
              this.formData.pointId = allData.pointId
              this.zkIsShow = true
            } else {
              this.zkIsShow = false
            }
            this.$nextTick(() => {
              this.$refs.formData.clearValidate()
            })
          } else {
            this.$message.error('获取详情失败')
          }
        })
        .catch(() => {
          this.$message.error('获取数据失败')
        })
    },
    typeSelectChange(val) {
      if (val === 'zk') {
        this.zkIsShow = true
      } else {
        this.zkIsShow = false
      }
    },
    getPoints(companyId) {
      this.axios({
        method: 'post',
        url: 'api-att/nc/attrule/expand/findpointbycomp',
        data: { companyId: companyId },
      })
        .then((res) => {
          if (!res.data.hasError) {
            this.points = res.data.result
          }
        })
        .catch(() => {
          this.$message.error('获取数据失败')
        })
    },
    showDetail(rows) {
      if ('zk' === rows.type) {
        this.$refs.zktecoMachine.showZktecoDetail(
          rows.projectId,
          rows.pointId,
          rows.machineNo,
          rows.projectName
        )
      } else if ('yf' === rows.type) {
        this.$refs.yfMachine.showYfDetail(rows, 'NC')
      }
    },
  },
}
</script>

<style scoped lang="less">
.container {
  .selectWidth {
    width: 100%;
  }
  .listAddBtn {
    width: 136px;
    background: #009588;
    border: none;
    position: absolute;
    right: 24px;
    top: 7px;
    height: 36px;
  }
  .listAddBtn:hover {
    background: #009588;
    border-color: #009588;
    opacity: 0.8;
  }
}
@import '../../../static/css/filterArea.less';
</style>
